package com.example.movie.service.impl;

import com.example.movie.dao.TPlayRecordDao;
import com.example.movie.dao.TVideoDao;
import com.example.movie.entity.TPlayRecord;
import com.example.movie.entity.TVideo;
import com.example.movie.service.TVideoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * (TVideo表)服务实现类
 *
 * @author makejava
 * @since 2021-04-02 16:51:15
 */
@Service("tVideoService")
public class TVideoServiceImpl implements TVideoService {
    @Resource
    private TVideoDao tVideoDao;
    @Resource
    private TPlayRecordDao tPlayRecordDao;
    /**
     * 通过ID查询单条数据
     *
     * @param videoId 主键
     * @return 实例对象
     */
    @Override
    public TVideo selectById(String videoId) {
        return this.tVideoDao.selectById(videoId);
    }

    /**
     * 分页查询
     *
     * @param start 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<TVideo> selectPage(int start, int limit) {
        return this.tVideoDao.selectPage(start, limit);
    }

    /**
     * 查询所有
     *
     * @return 实例对象的集合
     */
    @Override
    public List<TVideo> selectAll() {
        return this.tVideoDao.selectAll();
    }

    /**
     * 根据条件查询
     *
     * @return 实例对象的集合
     */
    @Override
    public List<TVideo> selectList(TVideo tVideo) {
        return this.tVideoDao.selectList(tVideo);
    }

    /**
     * 新增数据
     *
     * @param tVideo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(TVideo tVideo) {
        return this.tVideoDao.insert(tVideo);
    }

    /**
     * 批量新增
     *
     * @param tVideos 实例对象的集合
     * @return 生效的条数
     */
    @Override
    public int batchInsert(List<TVideo> tVideos) {
        return this.tVideoDao.batchInsert(tVideos);
    }

    /**
     * 修改数据
     *
     * @param tVideo 实例对象
     * @return 实例对象
     */
    @Override
    public TVideo update(TVideo tVideo) {
        this.tVideoDao.update(tVideo);
        return this.selectById(tVideo.getVideoId());
    }

    /**
     * 通过主键删除数据
     *
     * @param videoId 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(String videoId) {
        return this.tVideoDao.deleteById(videoId);
    }

    /**
     * 查询总数据数
     *
     * @return 数据总数
     */
    @Override
    public int count() {
        return this.tVideoDao.count();
    }

    @Override
    public int countList(TVideo tVideo) {
        return this.tVideoDao.countList(tVideo);
    }

    /**
     * 获取热门电影
     * @return
     */
    @Override
    public List<TVideo> getHotMovie() {
        return this.tVideoDao.getHotMovie();
    }

    @Override
    public List<TVideo> getLove(TVideo tVideo) {
        return this.tVideoDao.selectLove(tVideo);
    }

    @Override
    public List<TVideo> selectByName(TVideo tVideo) {
        return this.tVideoDao.selectByName(tVideo);
    }


    /**
     * 记录播放记录
     * @param playRecord
     * @return
     */
    @Override
    public int addTPlayRecord(TPlayRecord playRecord) {
        int result = 0;
        //判断用户对应的电影是否存在播放记录
        TPlayRecord tPlayRecord = this.tPlayRecordDao.getTPlayRecord(playRecord);
        if(tPlayRecord != null){
            result = this.tPlayRecordDao.updatePlayRecord(playRecord);
        }else{
            playRecord.setRecordId(UUID.randomUUID().toString().replace("-",""));
            result = this.tPlayRecordDao.addTPlayRecord(playRecord);
        }
        return result;
    }

    /**
     * 获取播放记录
     * @param playRecord
     * @return
     */
    @Override
    public TPlayRecord getTPlayRecord(TPlayRecord playRecord) {
        return this.tPlayRecordDao.getTPlayRecord(playRecord);
    }
}
